package jdbclianxi20250325;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * 执行DQL语句
 */
public class JDBCDemo5 {
    public static void main(String[] args) {
        //需求:查询teacher表中id,name,salary,title字段的信息
        try( Connection connection = DBUtil.getConnection()) {
            Statement statement = connection.createStatement();
            String sql = "select id,name,salary,title " +
                    "from teacher";
            ResultSet resultSet = statement.executeQuery(sql);
            while(resultSet.next()){
//                int id = resultSet.getInt("id");
//                String name = resultSet.getString("name");
//                int salary = resultSet.getInt("salary");
//                String title = resultSet.getString("title");
                int id = resultSet.getInt(1);
                String name = resultSet.getString(2);
                int salary = resultSet.getInt(3);
                String title = resultSet.getString(4);

                System.out.println(id+","+name+","+salary+","+title);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
